package com.lemon.smartsafety.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.data.app.DataUtil;
import com.lemon.smartsafety.entity.event.main.drawer.DrawerClickEvent;
import com.lemon.smartsafety.entity.menu.DrawItem;
import com.lemon.smartsafety.utils.app.SharedPreferencesHelper;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Cooliean on 16/1/19.
 */
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.DrawerItemViewHolder> {
    private LoggerHelper loggerHelper = new LoggerHelper(DrawerAdapter.class);
    private LayoutInflater mInflater;
    private int            mSelectedPosition;

    private static final int TOPVIEW_POSITION    = 0;
    private static final int BOTTOMVIEW_POSITION = 1;
    private SharedPreferencesHelper mSharedPreferencesHelper;

    private Context mContext;

    private List<DrawItem> items = DataUtil.getDrawMenuItems();

    public DrawerAdapter(Context context) {
        super();
        this.mContext = context;
        mSharedPreferencesHelper = new SharedPreferencesHelper(IApplication.context());
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public DrawerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DrawerItemViewHolder(mInflater.inflate(R.layout.item_drawer_item, parent, false));
    }

    @Override
    public void onBindViewHolder(DrawerItemViewHolder holder, int position) {
        holder.update(position);
    }

    @Override
    public int getItemCount() {
        return DataUtil.getDrawMenuItems().size();
    }

    protected class DrawerItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.drawer)
        View      drawer;
        @Bind(R.id.top_divider)
        View      top_divider;
        @Bind(R.id.bottom_divider)
        View      bottom_divider;
        @Bind(R.id.drawer_icon)
        ImageView iconIV;
        @Bind(R.id.drawer_name)
        TextView  nameTV;


        public DrawerItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        public void update(int position) {
            DrawItem item = items.get(position);
            if (position == TOPVIEW_POSITION) {
                top_divider.setVisibility(View.VISIBLE);
            } else if (position == BOTTOMVIEW_POSITION) {
                bottom_divider.setVisibility(View.VISIBLE);
            }

            iconIV.setImageResource(item.getIconRes());
            nameTV.setText(item.getTitle());

            if (mSelectedPosition == position) {
                drawer.setSelected(true);
            } else {
                drawer.setSelected(false);
            }
        }


        @Override
        public void onClick(View v) {
            DrawerClickEvent drawerClickEvent = new DrawerClickEvent(DataUtil.getDrawMenuItems().get(getLayoutPosition()).getTitle());
            drawerClickEvent.setEventResult(Constants.Result.ERROR);
            AppService.getBus().post(drawerClickEvent);
            mSelectedPosition = getLayoutPosition();
            notifyDataSetChanged();
        }
    }

    public void setSelectedPosition(int selectedPosition) {
        this.mSelectedPosition = selectedPosition;
        notifyDataSetChanged();
    }

    /**
     * 点击设置
     */
    public void clickProfile() {
        if (mSharedPreferencesHelper.isLogin()) {
            DrawerClickEvent drawerClickEvent = new DrawerClickEvent(DataUtil.getDrawMenuItems().get(items.size()-1).getTitle());
            drawerClickEvent.setEventResult(Constants.Result.ERROR);
            AppService.getBus().post(drawerClickEvent);
            mSelectedPosition = items.size()-1;
            notifyDataSetChanged();
        }else{
            UIHelper.showLoginActivity(this.mContext);
        }

    }
}
